<template>
  <div class="housing-management-wrapper">
    <p class="title">
      综合条件查询
      <el-link class="advance-search-btn" type="primary" :underline="false" @click="toggleAdvance">
        高级搜索<i class="el-icon-caret-bottom el-icon--right"></i>
      </el-link>
    </p>
    <div class="form-search-area">
      <el-form :inline="true" :model="formInline" size="medium" class="demo-form-inline">
        <el-form-item label="房主姓名">
          <el-input v-model="formInline.houseOwnerName" placeholder="请输入房主姓名" clearable></el-input>
        </el-form-item>
        <el-form-item label="房主联系方式">
          <el-input v-model="formInline.houseOwnerContact" placeholder="请输入房主联系方式" clearable></el-input>
        </el-form-item>
        <el-form-item label="房主身份证号码">
          <el-input v-model="formInline.cardNo" placeholder="请输入房主身份证号码" clearable></el-input>
        </el-form-item>
        <el-form-item label="房屋编号" v-show="advanceStatus">
          <el-input v-model="formInline.houseNo" placeholder="请输入房屋编号" clearable></el-input>
        </el-form-item>
        <el-form-item label="建筑用途" v-show="advanceStatus">
          <el-select v-model="formInline.buildingUse" placeholder="请选择建筑用途" clearable>
            <el-option
              v-for="(item) in DICY.buildingUse"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="建筑面积范围" v-show="advanceStatus">
          <el-row style="max-width:240px;">
            <el-col :span="11">
              <el-input v-model="formInline.buildingAreaMin" placeholder="最小面积" clearable></el-input>
            </el-col>
            <el-col :span="2" style="text-align:center;">-</el-col>
            <el-col :span="11">
              <el-input v-model="formInline.buildingAreaMax" placeholder="最大面积" clearable></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="出租用途" v-show="advanceStatus">
          <el-select v-model="formInline.rentUse" placeholder="请选择出租用途" clearable>
            <el-option
              v-for="(item) in DICY.rentUse"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="隐患类型" v-show="advanceStatus">
          <el-select v-model="formInline.hiddenDanger" placeholder="请选择隐患类型" clearable>
            <el-option
              v-for="(item) in DICY.hiddenDanger"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="承租人姓名" v-show="advanceStatus">
          <el-input v-model="formInline.tenantName" placeholder="请输入承租人姓名" clearable></el-input>
        </el-form-item>
        <el-form-item label="承租人联系方式" v-show="advanceStatus">
          <el-input v-model="formInline.tenantContact" placeholder="请输入承租人联系方式" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="medium" @click="searchOrg">查询</el-button>
          <el-button size="medium" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="right-btn-area">
        <el-button size="medium" type="primary" @click="popNewAddDialog">新增房屋</el-button>
        <el-button size="medium">导入</el-button>
        <el-button size="medium">导出</el-button>
        <!-- <el-button size="medium" @click="batchDeletion">批量删除</el-button> -->
      </div>
    </div>
    <!-- 表格 开始-->
    <div class="table-area clearfix">
      <el-table :data="tableData" ref="multipleTable" @selection-change="selectionChange" v-loading="loading" stripe>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" width="50">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column label="房屋编号" prop="houseNo"></el-table-column>
        <el-table-column label="房主姓名" prop="houseOwnerName"></el-table-column>
        <el-table-column label="房主联系方式" prop="houseOwnerContact"></el-table-column>
        <el-table-column label="建筑用途" prop="buildingUse">
          <template slot-scope="scope">{{convertKeyToVal(scope.row.buildingUse, DICY.buildingUse)}}</template>
        </el-table-column>
        <el-table-column label="建筑面积" prop="buildingArea"></el-table-column>
        <el-table-column label="操作" min-width="90">
          <template slot-scope="scope">
            <el-link type="primary" :underline="false" icon="el-icon-view" @click="viewDetail(scope.row.id)">查看</el-link>
            <el-link type="success" :underline="false" icon="el-icon-edit" @click="editDetail(scope.row.id)">编辑</el-link>
            <el-link type="danger" :underline="false" icon="el-icon-delete" @click="deleteDetail(scope.row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="paging-self"
        background
        :current-page.sync="currentPage"
        @current-change="pageChange"
        layout="total, prev, pager, next"
        :total="totalCount">
      </el-pagination>
    </div>
    <!-- 表格 结束-->
    <!-- 对话框 开始 -->
    <el-dialog title="房屋" :visible.sync="dialogFormVisible" width="880px" @closed="closeDialog">
      <el-form :model="dialogForm" class="dialog-form-comot" :disabled="dialogMode===CNST.READ_MODE" :rules="rules" ref="ruleDialog">
        <el-row>
          <el-col :span="12">
            <el-form-item label="房屋编号" label-width="154px" prop="houseNo">
              <el-input v-model="dialogForm.houseNo" placeholder="请输入房屋编号" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房屋地址" label-width="154px" prop="houseAddress">
              <el-input v-model="dialogForm.houseAddress" placeholder="请输入房屋地址" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="建筑用途" label-width="154px" prop="buildingUse">
              <el-select v-model="dialogForm.buildingUse" placeholder="请选择建筑用途" clearable>
                <el-option
                  v-for="(item) in DICY.buildingUse"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建筑面积(㎡)" label-width="154px" prop="buildingArea">
              <el-input v-model="dialogForm.buildingArea" placeholder="请输入建筑面积" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="证件代码" label-width="154px" prop="cardCode">
              <el-input v-model="dialogForm.cardCode" placeholder="请输入证件代码" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="证件号码" label-width="154px" prop="cardNo">
              <el-input v-model="dialogForm.cardNo" placeholder="请输入证件号码" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="房主姓名" label-width="154px" prop="houseOwnerName">
              <el-input v-model="dialogForm.houseOwnerName" placeholder="请输入房主姓名" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房主联系方式" label-width="154px" prop="houseOwnerContact">
              <el-input v-model="dialogForm.houseOwnerContact" placeholder="请输入房主联系方式" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="房主现居详址" label-width="154px" prop="houseOwnerAddress">
              <el-input v-model="dialogForm.houseOwnerAddress" type="textarea" placeholder="请输入房主现居详址" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出租用途" label-width="154px" prop="rentUse">
              <el-select v-model="dialogForm.rentUse" placeholder="请选择出租用途" clearable>
                <el-option
                  v-for="(item) in DICY.rentUse"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="隐患类型" label-width="154px" prop="hiddenDanger">
              <el-select v-model="dialogForm.hiddenDanger" placeholder="请选择隐患类型" clearable>
                <el-option
                  v-for="(item) in DICY.hiddenDanger"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="承租人公民身份号码" label-width="154px" prop="tenantIdCard">
              <el-input v-model="dialogForm.tenantIdCard" placeholder="请输入承租人公民身份号码" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="承租人姓名" label-width="154px" prop="tenantName">
              <el-input v-model="dialogForm.tenantName" placeholder="请输入承租人姓名" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="承租人联系方式" label-width="154px" prop="tenantContact">
              <el-input v-model="dialogForm.tenantContact" placeholder="请输入承租人联系方式" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" label-width="154px" prop="longitude">
              <el-input v-model="dialogForm.longitude" placeholder="请输入经度" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" label-width="154px" prop="latitude">
              <el-input v-model="dialogForm.latitude" placeholder="请输入纬度" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitOrg" v-if="dialogMode!==CNST.READ_MODE">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 对话框 结束 -->
  </div>
</template>

<script>
import {
  getGovernHouseList,
  addGovernHouse,
  getGovernHouseDetail,
  updateGovernHouseDetail,
  deleteGovernHouse,
  deleteGovernHouseBatch
} from '@/api/baseBusiness'
import { deepClone } from '@/utils'
import { mapState } from 'vuex'
export default {
  name: 'HousingManagement',
  data () {
    return {
      advanceStatus: false,
      formInline: {
        houseOwnerName: '',
        houseOwnerContact: '',
        cardNo: '',
        houseNo: '',
        buildingUse: '',
        buildingAreaMin: '',
        buildingAreaMax: '',
        rentUse: '',
        hiddenDanger: '',
        tenantName: '',
        tenantContact: ''
      },
      loading: false,
      tableData: [],
      multipleSelection: [],
      currentPage: 1,
      totalCount: 0,
      dialogFormVisible: false,
      dialogForm: {
        houseNo: '',
        houseAddress: '',
        buildingUse: '',
        buildingArea: '',
        cardCode: '',
        cardNo: '',
        houseOwnerName: '',
        houseOwnerContact: '',
        houseOwnerAddress: '',
        rentUse: '',
        hiddenDanger: '',
        tenantIdCard: '',
        tenantName: '',
        tenantContact: '',
        longitude: '',
        latitude: ''
      },
      rules: {
        houseNo: [{ required: true, message: '请输入 房屋编号', trigger: 'blur' }],
        houseAddress: [{ required: true, message: '请输入 房屋地址', trigger: 'blur' }],
        cardCode: [{ required: true, message: '请输入 证件代码', trigger: 'blur' }],
        cardNo: [{ required: true, message: '请输入 证件号码', trigger: 'blur' }],
        houseOwnerName: [{ required: true, message: '请输入 房主姓名', trigger: 'blur' }],
        houseOwnerContact: [{ required: true, message: '请输入 房主联系方式', trigger: 'blur' }],
        houseOwnerAddress: [{ required: true, message: '请输入 房主现居详址', trigger: 'blur' }],
        rentUse: [{ required: true, message: '请选择 出租用途', trigger: 'change' }],
        hiddenDanger: [{ required: true, message: '请选择 隐患类型', trigger: 'change' }],
        tenantIdCard: [{ required: true, message: '请输入 承租人公民身份号码', trigger: 'blur' }],
        tenantName: [{ required: true, message: '请输入 承租人姓名', trigger: 'blur' }],
        tenantContact: [{ required: true, message: '请输入 承租人联系方式', trigger: 'blur' }]
      },
      dialogMode: '', // 对话框的状态，新增，查看，编辑
      curId: '', // 点击查看或编辑的当前列表项的id
      options: this.DICY.jilian
    }
  },
  computed: {
    ...mapState([
      'areaId'
    ])
  },
  methods: {
    toggleAdvance () {
      // this.$refs.advancedForm.classList.toggle('shrink')
      this.advanceStatus = !this.advanceStatus
    },
    convertKeyToVal (code, arr) {
      arr = Array.isArray(arr) ? arr : []
      let resArr = arr.filter((item) => {
        return item.code === code
      })
      if (resArr.length === 0) {
        return ''
      }
      return resArr[0].name
    },
    searchOrg () {
      this.currentPage = 1
      this.getOrgListAndShow()
    },
    resetSearch () {
      for (let key in this.formInline) {
        this.$set(this.formInline, key, '')
      }
      this.currentPage = 1
      this.getOrgListAndShow()
    },
    popNewAddDialog () {
      this.dialogFormVisible = true
      this.dialogMode = this.CNST.ADDNEW_MODE
    },
    batchDeletion () {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请勾选要删除的项!'
        })
      } else {
        var ids = this.multipleSelection.map((item) => {
          return item.id
        }).join(',')
        this.$confirm('确定删除, 是否继续?', '批量删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteGovernHouseBatch({ ids }).then((res) => {
            this.$message({
              type: 'success',
              message: '批量删除成功!'
            })
            this.getOrgListAndShow()
          })
        })
      }
    },
    selectionChange (valArr) {
      this.multipleSelection = valArr
    },
    showToDialog (data) {
      for (let key in data) {
        if (this.dialogForm.hasOwnProperty(key)) {
          this.$set(this.dialogForm, key, data[key])
        }
      }
    },
    viewDetail (id) {
      this.dialogMode = this.CNST.READ_MODE
      this.dialogFormVisible = true
      getGovernHouseDetail({ id }).then((res) => {
        this.showToDialog(res.data)
      })
    },
    editDetail (id) {
      this.dialogMode = this.CNST.EDIT_MODE
      this.dialogFormVisible = true
      this.curId = id
      getGovernHouseDetail({ id }).then((res) => {
        this.showToDialog(res.data)
      })
    },
    deleteDetail (id) {
      this.$confirm('确定删除, 是否继续?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((res) => {
        deleteGovernHouse({ id }).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getOrgListAndShow()
        })
      })
    },
    pageChange (curPage) {
      this.currentPage = curPage
      this.getOrgListAndShow()
    },
    submitOrg () {
      this.$refs.ruleDialog.validate((valid) => {
        if (valid) {
          // -----------------
          let requestData = deepClone(this.dialogForm)
          if (this.dialogMode === this.CNST.ADDNEW_MODE) {
            // 新增
            requestData.areaId = this.areaId
            addGovernHouse(requestData).then((res) => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.getOrgListAndShow()
              this.dialogFormVisible = false
            })
          } else if (this.dialogMode === this.CNST.EDIT_MODE) {
            // 修改
            requestData.id = this.curId
            updateGovernHouseDetail(requestData).then((res) => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.getOrgListAndShow()
              this.dialogFormVisible = false
            })
          }
          // -----------------
        } else {
          return false
        }
      })
    },
    async getOrgListAndShow () {
      try {
        this.loading = true
        let requestData = {
          page: this.currentPage,
          areaId: this.areaId,
          ...deepClone(this.formInline)
        }
        var res = await getGovernHouseList(requestData)
        this.tableData = res.data.queryResult
        this.totalCount = res.data.totalCount
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false
      }
    },
    closeDialog () {
      // 当前点击查看或编辑列表项的id置空
      this.curId = ''
      // 弹框关闭后重置表单验证
      this.$refs.ruleDialog.resetFields()
      // 清空弹框内表单数据
      for (let key in this.dialogForm) {
        this.dialogForm[key] = ''
      }
    },
    handleChange (value) {
      console.log(value)
    }
  },
  mounted () {
    this.getOrgListAndShow()
  }
}
</script>

<style lang="scss" scoped>
.housing-management-wrapper {
  background-color: #ffffff;
  margin-top: 12px;
  border-radius: 4px;
  box-shadow: 10px 10px 22px -2px #d2d6da;
  padding: 20px;
  .title {
    margin: 0;
    font-size: 15px;
    color: #333333;
    margin-bottom: 10px;
    .advance-search-btn {
      float: right;
      user-select: none;
    }
  }
  .form-search-area {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .right-btn-area {
      height: 58px;
    }
  }
  .table-area {
    .el-link {
      margin-right: 13px;
    }
    .el-pagination.paging-self {
      margin-top: 20px;
      float: right;
    }
  }
  .el-form.dialog-form-comot {
    .el-form-item {
      .el-cascader,.el-select,.el-input-number {
        width: 100%;
      }
      .el-date-editor.el-input,.el-date-editor.el-input__inner {
        width: 100%;
      }
    }
  }
}
</style>
